const { toUrl,globalData } = getApp();
import { request } from "@/utils/request";
Page({
  toUrl,
  data: {
    getBannerlist: [],
    getInFo: {},
    type: 1,
    pages: 1,
    isEnd: false,
    count:0,
    globalData,
    userInfo:{}
  },
  getBannerlist(type = this.data.type) {
    if (this.data.pages > 1 && this.data.pages > this.data.count) {
      this.setData({
        isEnd: true,
      });
      return;
    }
    const data = {
      type: type,
      page: this.data.pages,
    };
    request("integral/integrallist", { data }).then((res) => {
      console.log(res);
      let pages = this.data.pages + 1
      if (this.data.pages > 1) {
        let list = this.data.getBannerlist
        res.data.forEach((it, idx) => {
          list.push(it);
        });
        this.setData({
          getBannerlist: list,
          pages,
          count:res.count
        });
      } else {
        this.setData({
          getBannerlist: res.data,
          pages,
          count:res.count

        });
      }
    });
  },
  getInFo() {
    request("integral/index").then((res) => {
      console.log(res);
      this.setData({
        getInFo: res.data,
      });

      request("user/getuserinfo").then((res) => {
        if (res.errcode == 1) {
          console.log('用户信息');
          getApp().globalData.baseInfo = res.data
          this.setData({
            userInfo:getApp().globalData.baseInfo
          })
        }
      });
    });
  },
  getTypes(data) {
    //类型切换
    let { type } = data.currentTarget.dataset;
    this.setData({
      type: type,
      pages: 1,
    });
    this.getBannerlist(type);

  },
  more() {
    let pages = this.data.pages + 1;
    console.log(pages);
    this.setData({
      pages: pages,
    });
    this.getBannerlist();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      userInfo:getApp().globalData.baseInfo
    })
    this.getBannerlist();
    this.getInFo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let isEnd = this.data.isEnd
    if (isEnd) {
      return
    }else{
      this.getBannerlist()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
